import pandas as pd
import numpy as np
import os
import sys
import model_code.gen_data as gd
import model_code.rfg as rfg
import model_code.per_mea as pm
from tensorflow import keras
import shap
from sklearn.model_selection import train_test_split

source = "tongji" #tongji/vital
pre_time = "5"#5 10 15
ioh_time = "1" #1
ob_win = "5" #5 10 15

pre_time = int(pre_time)
pre_time = pre_time / 5
ob_win = int(ob_win)

d_path = source + "/dynamic_normalization/" + ioh_time + "-bt.csv"
c_path = "config_bt.json"

s_path_train =  source + "/" + "static/train_case.csv"
s_path_test =  source + "/" + "static/test_case.csv"

train_static, train_dynamic, train_label = gd.gen_data(source, d_path, c_path, s_path_train, pre_time, ob_win)
test_static, test_dynamic, test_label = gd.gen_data(source, d_path, c_path, s_path_test, pre_time, ob_win)

r = train_label.sum()
l = train_label.shape[0] - r
r = l / r
if r < 1:
    r = 1
l = 1
cw = {0: l, 1: r}
print("cw: 1, " + str(r))

train_dynamic_dim = train_dynamic.reshape(train_dynamic.shape[0], train_dynamic.shape[1], train_dynamic.shape[2], 1)
test_dynamic_dim = test_dynamic.reshape(test_dynamic.shape[0], test_dynamic.shape[1], test_dynamic.shape[2], 1)

shap.initjs()
model_path = "models/tongji-1.0-1-5.h5"
model = keras.models.load_model(model_path)

explainer = shap.GradientExplainer(model,[train_dynamic_dim[250:300], train_dynamic[250:300]])
shap_values = explainer.shap_values([test_dynamic_dim[550:600], test_dynamic[550:600]])
print("shap_values",shap_values)